package io.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;

/**
 * socket 客户端
 *
 * @author verlet
 * @date 2020/2/28
 */
@Slf4j
public class Client {

    private void send(String hostname, int port) throws IOException, InterruptedException {
        Socket socket = new Socket();
        SocketAddress socketAddress = new InetSocketAddress(hostname, port);
        socket.connect(socketAddress, 30 * 1000);
        OutputStream outputStream = socket.getOutputStream();
        log.info("启动成功，连接上服务器。。。");
        while (true) {
            outputStream.write("我来了！\n".getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            Thread.sleep(1000L);
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        new Client().send("127.0.0.1", 5201);
    }
}
